﻿package DevilCat{//DelePokemon
	import flash.display.MovieClip;
	import flash.net.SharedObject;
	import flash.text.TextField;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import flash.display.SimpleButton;    
	import flash.utils.getDefinitionByName;
	import DevilCat.SQLData;
	import DevilCat.PokemonObject;
	import DevilCat.ItemObject;
	import DevilCat.FloatingText;
	import DevilCat.PokemonAttack;

	public class PokemonBattle extends MovieClip {
		private var eventLocation:Number=0;
		private var eventNumber:Number=0;		
		private var tweenList:Array;
		private var catched:Boolean=false;
		private var manualSwitch:Boolean=true;
		private var isTrainer:Boolean=false;
		private var moveIsShowed:Boolean=false;
		private var itemIsShowed:Boolean=false;
		private var pokemonIsShowed:Boolean=false;
		private var floatSpace:Number=17;
		private var floatTime:Number=2;
		private var weather:String="None";
		private var weatherTurn:Number=-1;
		
		private var TargetList:Array;
		public var MyPokemon:PokemonAttack;
		private var MyPokemonCurrentID:Number;
		public var TargetPokemon:PokemonAttack;
		
		private var myOldHP:Number=0;

		public function PokemonBattle(_TargetList:Array,_isTrainer:Boolean=false,addStat:Number=0) {
			SQLData.battleClip=this;
			TargetList=_TargetList;
				SQLData.mainClip.AfterBattle("_TargetList "+_TargetList);
			isTrainer=_isTrainer;
			FreeTween();			
			tweenList.unshift( new Tween (this,"alpha",Elastic.easeOut,0,1,1,true));
			EnableMenu();
			DelPokemonPanel.visible=false;
			//LoadMoves();	
			var PokemonAlive:Number=CheckPokemonList(SQLData.pokemonList);
			if(PokemonAlive<0){
				SQLData.mainClip.AfterBattle("failed");
				this.parent.removeChild(this);
			}else{
				MyPokemonCurrentID=SQLData.pokemonList[PokemonAlive].upid;
				MyPokemon=new PokemonAttack(SQLData.pokemonList[PokemonAlive]);
				AddTargetPokemon();			
				AddMyPokemon();
			}			
		}		
		public function ThrowBall(ballName:String) {
SQLData.mainClip.Trace("ThrowBall "+ballName);
			TargetPokemon.ClearAuto();
			MyPokemon.ClearAuto();
			var temRate:Number=TargetPokemon.pokemon.Rate;
			var HPRate:Number=TargetPokemon.pokemon.CurrentHP/TargetPokemon.pokemon.HP;
			if(HPRate < 1/8) 				temRate-=3;
			else if(HPRate < 1/4) 			temRate-=2;
			else if(HPRate < 1/2) 			temRate-=1;
			if(ballName=="ultraball")		temRate-=2;
			else if(ballName=="greatball")	temRate--;
			if(TargetPokemon.pokemon.Status=="Sleep" || TargetPokemon.pokemon.Status=="Frozen")temRate--;
			if(temRate<1)temRate=1;
			//temRate=0;
			var catchNumber:Number=Math.round(Math.random()*temRate);
			if(catchNumber==1)catched=true;
			else catched=false;
			Balls.gotoAndStop(ballName);
			tweenList.unshift(new Tween (Balls,"alpha",Strong.easeOut,0,1,1,true));
			tweenList.unshift(new Tween (Balls,"x",Strong.easeOut,430,700,1,true));
			tweenList.unshift(new Tween (Balls,"y",Strong.easeOut,220,190,1,true));
			tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,ballOpen);
			function ballOpen(e:Event){				
				tweenList.unshift(new Tween (TargetPokemon,"alpha",Strong.easeOut,1,0,0.5,true));
				tweenList.unshift(new Tween (Balls,"x",Strong.easeOut,700,650,1,true));
				tweenList.unshift(new Tween (Balls,"scaleX",Strong.easeOut,1,4,0.5,true));
				tweenList.unshift(new Tween (Balls,"scaleY",Strong.easeOut,1,4,0.5,true));
				tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,ballClose);
				
			}
			function ballClose(e:Event){				
				tweenList.unshift(new Tween (Balls,"scaleY",Strong.easeOut,4,1,0.5,true));
				tweenList.unshift(new Tween (Balls,"scaleX",Strong.easeOut,4,1,0.5,true));
				tweenList.unshift(new Tween (Balls,"y",Strong.easeOut,190,245,1,true));
				tweenList.unshift(new Tween (Balls,"x",Strong.easeOut,650,700,1,true));
				tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,ballCheck);
			}
				
			function ballCheck(e:Event){			
			MyPokemon.UsingItem=false;	
			//catched=true;
				if(catched){
					TargetPokemon.RemoveMe();
					tweenList.unshift(new Tween (Balls,"x",Strong.easeOut,700,430,1,true));							
					if(SQLData.pokemonList.length<6){							
						SQLData.pokemonList.push(TargetPokemon.pokemon);
						SQLData.InsertPokemonSQL(TargetPokemon.pokemon);
						RemoveBattle();
					}else{
						addChild(new FloatingText("BAN KHONG THE CO NHIEU HON 6 POKEMON!",310,155,2));
						DisableMenu();
						HideAllPanel();
						LoadDelPokemonList();
					}
				
				}else{
					TargetPokemon.alpha=1;
					TargetPokemon.AI();
				}
				tweenList.unshift(new Tween (Balls,"alpha",Strong.easeOut,1,0,1,true));
				
			}
		}
		private function RemoveBattle() {
			MyPokemon.RemoveMeTween();
			TargetPokemon.RemoveMeTween();
			tweenList.unshift(new Tween (this,"alpha",Regular.easeOut,1,0,1,true));
			tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,BackToGame);
		}
		private function BackToGame(e:Event) {						
			if (this.parent != null) {
				var MyList:int=CheckPokemonList(SQLData.pokemonList);
				var TargetList:int=CheckPokemonList(TargetList);
				SQLData.mainClip.Trace("MyList "+MyList+" TargetList "+TargetList);
				if(MyList<0){
					SQLData.mainClip.AfterBattle("failed");
				}else
				if (TargetList<0 && MyPokemon.pokemon.CurrentHP>0) {
					SQLData.mainClip.AfterBattle("win");
					if(eventLocation>0){
						SQLData.changeUserEvent(eventLocation,eventNumber);
					}
					SQLData.changePMP(1);
				}else
				if(TargetList>=0 && MyList>=0){
					if(catched)
					SQLData.mainClip.AfterBattle("catched");
					else
					SQLData.mainClip.AfterBattle("flee");					
				}				
				this.parent.removeChild(this);
			}
		}
		private function Flee(e:Event) {
			DisableMenu();
			RemoveBattle();
		}
		function AddMyPokemon(){
			tweenList.unshift(new Tween (MyPokemon,"alpha",Regular.easeOut,0,1,1,true));
			MyPokemon.InitPokemon(true);
			//MyPokemon.SetStatus("Confuse");
			MyPokemon.x=300;
			MyPokemon.setTarget(TargetPokemon);
			PokemonBattleMC.addChild(MyPokemon);	
		}
		function AddTargetPokemon(){			
		var PokemonAlive:Number=CheckPokemonList(TargetList);
			TargetPokemon=new PokemonAttack(TargetList[PokemonAlive]);
			tweenList.unshift(new Tween (TargetPokemon,"alpha",Regular.easeOut,0,1,1,true));
			TargetPokemon.InitPokemon(false);
			TargetPokemon.x=600;
			TargetPokemon.setTarget(MyPokemon);
			TargetPokemon.AI();
			PokemonBattleMC.addChild(TargetPokemon);
		}

		////////////////////////////////////////BATTLE////////////////////////////////////////////
		public function SetEventBattle(_eventLocation:Number,_eventNumber:Number){			
			if(_eventLocation>0){
				eventLocation=_eventLocation;
				eventNumber=_eventNumber;			
			}
		}
		
		public function CheckPokemonList(PokemonList:Array):Number {
			for (var i:int=0; i<PokemonList.length; i++) {
				if (PokemonList[i].CurrentHP>0) {
					return i;
				}
			}
			return -1;
		}

		private function FreeTween() {
			tweenList=null;
			tweenList=new Array();
		}
		//---------------------MENU
		private function EnableMenu() {
			ShowPokemon.addEventListener(MouseEvent.CLICK,ShowPokemonPanelEvent);
			ShowItem.addEventListener(MouseEvent.CLICK,ShowItemPanelEvent);
			if(!isTrainer)OutBattle.addEventListener(MouseEvent.CLICK,Flee);
		}
		private function DisableMenu() {
			ShowPokemon.removeEventListener(MouseEvent.CLICK,ShowPokemonPanelEvent);
			ShowItem.removeEventListener(MouseEvent.CLICK,ShowItemPanelEvent);
			if(!isTrainer)OutBattle.removeEventListener(MouseEvent.CLICK,Flee);
		}
		private function HideAllPanel() {
			HideItemPanel();
			HidePokemonPanel();
		}
		private function ShowPokemonPanelEvent(e:MouseEvent) {
			manualSwitch=true;
			ShowPokemonPanel();
		}
		private function ShowPokemonPanel() {
			if (!pokemonIsShowed) {
				LoadPokemonList();
				HideAllPanel();				
				tweenList.unshift(new Tween (PokemonPanel,"y",Strong.easeOut,-272,-2,1,true));
				pokemonIsShowed=true;
				EnablePokemonSwitch();
			} else {
				HidePokemonPanel();
			}
		}
		private function HidePokemonPanel() {
			if (pokemonIsShowed) {				
				tweenList.unshift( new Tween (PokemonPanel,"y",Strong.easeOut,-2,-272,1,true));
				pokemonIsShowed=false;
				DisablePokemonSwitch();
			}
		}
		private function ShowItemPanelEvent(e:MouseEvent) {
			ShowItemPanel();
		}
		private function ShowItemPanel() {
			if (!itemIsShowed) {
				LoadItem();
				HideAllPanel();				
				tweenList.unshift(new Tween (ItemPanel,"y",Strong.easeOut,-272,-2,1,true));
				itemIsShowed=true;
				EnableItem();
			} else {
				HideItemPanel();
			}
		}
		private function HideItemPanel() {
			if (itemIsShowed) {
				DisableItem();
				
				tweenList.unshift( new Tween (ItemPanel,"y",Strong.easeOut,-2,-272,1,true));
				itemIsShowed=false;
			}
		}
		//---------------------ITEMS LIST
		private function LoadItem() {
			SQLData.LoadItemPanel(ItemPanel);
		}
		private function EnableItem() {
			ItemPanel.mouseChildren=true;
		}
		private function DisableItem() {
			ItemPanel.mouseChildren=false;
		}

		function LoadPokemonList() {			
			while (PokemonPanel.numChildren > 0) {
				PokemonPanel.removeChildAt(0);
			}
			for (var i:int =0; i<SQLData.pokemonList.length; i++) {
				var pokemon:PokemonInfor = new PokemonInfor(SQLData.pokemonList[i]);
				pokemon.y=24*i;
				pokemon.AddEventSwitchPokemon();
				PokemonPanel.addChild(pokemon);
			}
		}
		private function EnablePokemonSwitch() {
			SQLData.mainClip.Trace("EnablePokemonSwitch");
			PokemonPanel.addEventListener(MouseEvent.CLICK, ClickSwitchPokemon);
		}
		private function DisablePokemonSwitch() {
			PokemonPanel.removeEventListener(MouseEvent.CLICK, ClickSwitchPokemon);
		}
		private function LoadDelPokemonList() {			
			SQLData.mainClip.Trace("loadDelPokemonList");
			DelPokemonPanel.visible=true;
			MyPokemon.visible=false;
			var i:int;			
			while (DelPokemonPanel.numChildren > 0) {
				DelPokemonPanel.removeChildAt(0);
			}
			HideAllPanel();
			for ( i=0; i<SQLData.pokemonList.length; i++) {
				var pokemon:PokemonInfor = new PokemonInfor(SQLData.pokemonList[i]);
				pokemon.y=24*i;
				pokemon.AddEventReplacePokemon(TargetPokemon.pokemon);
				DelPokemonPanel.addChild(pokemon);
			}				
			var pokemonTargetInfor:PokemonInfor = new PokemonInfor(TargetPokemon.pokemon);
			pokemonTargetInfor.y=24*i+10;
			DelPokemonPanel.addChild(pokemonTargetInfor);			
			DelPokemonPanel.addEventListener(MouseEvent.CLICK, ClickDelPokemon);
		}
		private function ClickDelPokemon(evt:MouseEvent) {
			if (evt.target.pokemon.HP!=undefined) {		
				if(CheckPokemonList(TargetList)>=0){					
					PokemonFailed(true);
					AddTargetPokemon();
					MyPokemon.visible=true;
				}else
				RemoveBattle();		
			}
		}
		private function ClickSwitchPokemon(evt:MouseEvent) {
			LoadPokemonList();
			if(MyPokemonCurrentID!=evt.target.pokemon.upid && evt.target.pokemon.CurrentHP>0){		
SQLData.mainClip.Trace("MyPokemonCurrentID "+MyPokemonCurrentID+" evt.target.pokemon.upid "+evt.target.pokemon.upid);
				MyPokemonCurrentID=evt.target.pokemon.upid;		
				if(MyPokemon.pokemon.CurrentHP>0){
					TargetPokemon.ClearAuto();
					MyPokemon.RemoveMe();
					//PokemonBattleMC.removeChild(MyPokemon);
					MyPokemon=null;	
				}
				SQLData.mainClip.Trace("End New !");
				MyPokemon=new PokemonAttack(evt.target.pokemon);
				AddMyPokemon();
				TargetPokemon.setTarget(MyPokemon);
				TargetPokemon.AI();
				HideAllPanel();
			}
		}
		public function PokemonFailed(IsAI:Boolean){
			var PokemonAlive:Number;
			if(IsAI){
				PokemonAlive=CheckPokemonList(TargetList);
				if(PokemonAlive>0){
					TargetPokemon=new PokemonAttack(TargetList[PokemonAlive]);
					AddTargetPokemon();		
					MyPokemon.setTarget(TargetPokemon);
				}else{				
					RemoveBattle();
				}
			}else{				
				TargetPokemon.ClearAuto();
				PokemonAlive=CheckPokemonList(SQLData.pokemonList);
				if(PokemonAlive>0){
					if(!pokemonIsShowed)
						ShowPokemonPanel();
				}else{
					RemoveBattle();
				}
			}			
		}
	}
}